package com.yiren.manager.ctrl.equipment;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.FileUtil;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.PropertyConfigurer;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrAppHomeInfo;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.equipment.YrAppHomeInfoForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.equipment.YrAppHomeInfoService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("appHomeInfo")
@Controller
public class YrAppHomeInfoServiceController extends GenericController<YrAppHomeInfoForm, YrAppHomeInfo>{

	
	@Autowired
	private YrAppHomeInfoService selfService;
	
	@Autowired
	private YrStaffInfoService yrStaffInfoService;
	
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrAppHomeInfoForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrAppHomeInfoForm> result = new HTTPResult<YrAppHomeInfoForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrAppHomeInfoForm form = tool.mapper("appHomeInfo", YrAppHomeInfoForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getAppHomeInfoId()||form.getAppHomeInfoId().isEmpty()){
			form.setAppHomeInfoId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		if(null!=form.getImgBase64()&&!form.getImgBase64().isEmpty()){
			YrAppHomeInfo entity = selfService.findOne(form.getAppHomeInfoId());
			if(entity!=null){
				FileUtil.deleteFile(String.valueOf(PropertyConfigurer.getContextProperty("yiren.img.save.url"))+entity.getPhotoUrl());
			}
			form.setPhotoUrl(FileUtil.saveBase64(form.getImgBase64(),String.valueOf(PropertyConfigurer.getContextProperty("yiren.img.save.url"))));
		}
		YrAppHomeInfo entity = ObjectUtils.objectToObject(form, YrAppHomeInfo.class);
		selfService.save(entity);
		return result;
	}
	
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrAppHomeInfoForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrAppHomeInfoForm> result = new HTTPResult<YrAppHomeInfoForm>();
		List<YrAppHomeInfo> entityList = selfService.findAll();
		List<YrAppHomeInfoForm> formList = new ArrayList<YrAppHomeInfoForm>();
		for(YrAppHomeInfo entity : entityList){
			YrAppHomeInfoForm form = ObjectUtils.objectToObject(entity, YrAppHomeInfoForm.class);
			if(null!=form.getUpdateId()){
				YrStaffInfo staffInfo = yrStaffInfoService.findOne(form.getUpdateId());
				if(null!=staffInfo){
					form.setUpdateName(staffInfo.getName());
					form.setDepartmentName(staffInfo.getDepartmentName());
				}
			}
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrAppHomeInfoForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrAppHomeInfoForm> result = new HTTPResult<YrAppHomeInfoForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			selfService.delete(id);
		}
		return result;
	}
	
	
	@Override
	public YrAppHomeInfoForm toForm(YrAppHomeInfo e) throws Exception {
		YrAppHomeInfoForm form = ObjectUtils.objectToObject(e, YrAppHomeInfoForm.class);
		return form;
	}

	@Override
	public YrAppHomeInfo toEntity(YrAppHomeInfoForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
